<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Acting;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ActingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Acting(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('url');
            $grid->column('parameter')->pluck('value', 'name')->limit(20);
            $grid->column('exp_time')->editable();
            $grid->column('type')->select([1=>'国内',2=>'国外'], 'true');
            $grid->column('status')->switch();
            $grid->column('method')->using([0=>'GET',1=>'POST']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Acting(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('url');
            $show->field('parameter');
            $show->field('exp_time');
            $show->field('type');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Acting(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->url('url')->help('问号?后面的参数');
            $form->array('parameter',function (Form\NestedForm $form){
                $form->text('name','参数名');
                $form->text('value','参数值');
            })->saving(function ($v){
                return json_encode($v);
            });
            $form->table('restkey','响应参数',function (Form\NestedForm $form){
                $form->text('name','参数名');
            })->help('响应参数，一般只填ip和端口')->saving(function ($v){
                return json_encode($v);
            });
            $form->text('exp_time')->help('时间为天');
            $form->radio('type')->options([1=>'国内',2=>'国外'])->default(1);
            $form->radio('status')->options([1=>'开启',0=>'关闭'])->default(1);
            $form->radio('method')->options([1=>'POST',0=>'GET'])->default(0);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
    public function update($id)
    {
       if(request()->has('status')){
           $status=request()->get('status');
           if($status==1){
               $model=\App\Models\Acting::query()->whereId($id)->first();
               $count=$model->where('type',$model->type)->where('status',1)->count();
               if ($count>1){
                  return Form::make()->response()->error('该类型下已有开启的接口，如有需要请关闭其他接口')->refresh();
               }
           }
       }
        return parent::update($id);
    }
}
